package edu.mse.camel.client.uml.collab.editor;

import java.util.Iterator;

import org.eclipse.gef.ui.actions.ActionRegistry;
import org.eclipse.ui.IWorkbenchPart;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.helper.IModelerHelper;
import com.cea.papyrus.core.helper.ModelerHelperRegistry;
import com.cea.papyrus.core.treeeditpart.CreateElementActionWithCommand;

public class EditorUtil {

	@SuppressWarnings("unchecked")
	public static ActionRegistry createParentRegistry(IWorkbenchPart part) {
		// for each diagram, create "create actions"
		ActionRegistry parentActionRegistry = new ActionRegistry();
		ModelerHelperRegistry factoryRegistry = 
			PapyrusPlugin.getDefault().getFactoryRegistry();
		Iterator<IModelerHelper> it = factoryRegistry.getFactories().iterator();
		while(it.hasNext()) {
			IModelerHelper factory = it.next();
			Iterator<Class> metaclasses = factory.getDefinedMetaClasses().iterator();
			while(metaclasses.hasNext()) {
				Class metaclass = metaclasses.next();
				// if the action does not exist yet, creates a new one and add it to the repository
				if(parentActionRegistry.getAction(
						CreateElementActionWithCommand.getId(metaclass))== null) {
					parentActionRegistry.registerAction(
							new CreateElementActionWithCommand(part, metaclass));
				}
			}
		}
		return parentActionRegistry;
	}
	
}
